﻿using System;
using System.Collections.Generic;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Exposes an entity which contains 
    /// an audit information.
    /// </summary>
    public interface IAuditableEntity : IEntity
    {
        /// <summary>
        /// Gets or sets the creation date.
        /// </summary>
        DateTime CreatedOn
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the last update date.
        /// </summary>
        DateTime UpdatedOn
        {
            get;
            set;
        }
    }

    /// <summary>
    /// Represents the entity which contains 
    /// an audit information.
    /// </summary>
    public abstract class AuditableEntity : Entity, 
        IAuditableEntity
    {
        #region Fields

        private DateTime _createdOn;
        private DateTime _updatedOn;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        protected AuditableEntity()
        {
            _createdOn = DateTime.UtcNow;
            _updatedOn = DateTime.UtcNow;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the creation date.
        /// </summary>
        public virtual DateTime CreatedOn
        {
            get
            {
                return _createdOn;
            }
            set
            {
                _createdOn = value;
            }
        }

        /// <summary>
        /// Gets or sets the last update date.
        /// </summary>
        public virtual DateTime UpdatedOn
        {
            get
            {
                return _updatedOn;
            }
            set
            {
                _updatedOn = value;
            }
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj, 
            DisclosureLevel level, 
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["CreatedOn"] = CreatedOn;
                obj["UpdatedOn"] = UpdatedOn;
            }
        }

        #endregion
    }
}
